package forum_system_test;

import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author tongchen
 * @create 2023-08-19 16:55
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SpecificPageTest {
    private static ChromeDriver chromeDriver=new ChromeDriver();
    @BeforeAll
    public  static void loginAndOpenWeb() throws InterruptedException {
        chromeDriver.get("http://47.93.222.41:58080/sign-in.html");
        WebElement element = chromeDriver.findElement(By.cssSelector("#username"));
        WebElement element1 = chromeDriver.findElement(By.cssSelector("#password"));
        WebElement element2 = chromeDriver.findElement(By.cssSelector("#submit"));
        element.clear();
        element1.clear();
        element.sendKeys("刘健磊");
        element1.sendKeys("123456");
        //强制等待一秒，使新页面能加载出来
        element2.click();
        Thread.sleep(5000);
        //寻找具体元素并打开
        chromeDriver.findElement(By.cssSelector("#artical-items-body > div:nth-child(6) > div > div.col > div.text-truncate > a")).click();
    }
    @Order(1)
    @Test
    public void testElementExists() throws InterruptedException {
        //休眠一定时间，使元素有时间能加载出来
        Thread.sleep(2000);
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"));
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"));
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4)"));
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"));
        chromeDriver.findElement(By.cssSelector("#article_details_author_avatar"));
        chromeDriver.findElement(By.cssSelector("#article_details_author_name"));
        chromeDriver.findElement(By.cssSelector("#details_article_content_title"));
        chromeDriver.findElement(By.cssSelector("#details_article_content > p"));
        chromeDriver.findElement(By.cssSelector("#details_btn_like_count"));
        chromeDriver.findElement(By.cssSelector("#details_artile_edit"));
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"));
        //查看回复列表对应元素是否存在
        chromeDriver.findElement(By.cssSelector("#details_reply_area > div:nth-child(2) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto > div > div > ul > li"));
        chromeDriver.findElement(By.cssSelector("#details_reply_area > div:nth-child(2) > div.col-3.card > div > span"));
        chromeDriver.findElement(By.cssSelector("#details_reply_area > div:nth-child(2) > div.col-3.card > div > div > a"));
        chromeDriver.findElement(By.cssSelector("#details_article_reply_content_36"));
        chromeDriver.findElement(By.cssSelector("#details_btn_article_reply > span"));
    }
    @Order(2)
    @Test
    public void testLikeClick() throws InterruptedException {
        //继续休眠、
        Thread.sleep(5000);
        //三个基本元素的点击
        String text = chromeDriver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        Integer agoNum = Integer.valueOf(text);
        agoNum+=1;
        String expectNum=agoNum+"";
        //在点击前后判断点赞数量是否+1
        chromeDriver.findElement(By.cssSelector("#details_btn_like_count")).click();
        //chromeDriver.executeScript("return document.getElementById(\"#details_btn_like_count\").click()");
        //阻塞一会使页面更新
        Thread.sleep(2000);
        String numNow = chromeDriver.findElement(By.cssSelector("#details_article_likeCount")).getText();
        Assertions.assertEquals(expectNum, numNow);
////        chromeDriver.findElement(By.cssSelector("")).click();
//        chromeDriver.findElement(By.cssSelector("")).click();
        //测试2使赞的数量达到5000；
        for(int i=0;i<500000;++i){
            chromeDriver.findElement(By.cssSelector("#details_btn_like_count")).click();
        }
    }
    @Order(3)
    @Test
    public void clickDelete() throws InterruptedException {
        //阻塞三秒，使内容能够被完全加载出来
        Thread.sleep(3000);
        //定位删除按钮
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        //确认提示内容是否与预期值相同
        Thread.sleep(1000);
        //String realText = chromeDriver.switchTo().alert().getText();
        //String expectText="点击确认该帖子将被删除.";
        //比较两个帖子的内容是否相同
        //Assertions.assertEquals(expectText, realText);
        //点击删除按钮
        //chromeDriver.switchTo().alert().dismiss();
        //取消删除元素
        //点击取消按钮
        //测试结果（并不属于弹窗，所以不需要使用alert（））
        //涉及到页面内部进行切换，所以进行短暂的阻塞

        chromeDriver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a")).click();
        //紧接着测试回复按钮的测试
        Thread.sleep(5000);
        chromeDriver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("测试用例回复");
        //点击发布按钮进行发布
        chromeDriver.findElement(By.cssSelector("#details_btn_article_reply")).click();
    }
}
